package org.bao.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bao.utils.MyUtils;

import com.sun.glass.ui.Application;

import org.bao.bean.*;
import org.bao.service.impl.RegisterServiceImpl;

/**
 * Servlet implementation class UserRegister
 */
@WebServlet("/UserRegister")
public class UserRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取性别
//		String usersex =  request.getParameter("usersex");
//		System.out.println("usersex:"+usersex);
		// 获取表单数据
		UserRegisterB adminB = MyUtils.convertFormData2Bean(request, UserRegisterB.class);
		System.out.println("adminB:" + adminB.toString());
		// 将信息写入数据库中
		int i = MyUtils.getInstance(RegisterServiceImpl.class).saveOrUpdateUserreview(adminB);
		System.out.println("i:" + i);
		String istr = String.valueOf(i);

		PrintWriter out = response.getWriter();
		// 添加后的跳转
		if (i > 0) {
			//将电话写入session
			request.getSession().setAttribute("userTel", adminB.getUsertel());
			request.getSession().setAttribute("userName", adminB.getUsername());
			request.getSession().setAttribute("userSex", adminB.getUsersex());
			request.getSession().setAttribute("userPsw", adminB.getUserpsw());
			request.getSession().setAttribute("userLocation", adminB.getUserlocation());
			
			response.setContentType("text/html;charset=utf-8");
			
			//注册成功
			out.print("<script language='javascript'>alert('注册成功！');</script>");
			
			response.sendRedirect("cwx/account.jsp");
			//转发
//			request.getRequestDispatcher("cwx/account.jsp").forward(request, response);
		}else {
		// 保存失败
			//注册失败
			out.print("<script language='javascript'>alert('注册失败！');</script>");
			response.sendRedirect("cwx/registe.jsp");
		// 转发
//			request.getRequestDispatcher("cwx/registe.jsp").forward(request, response);

		}

	}
}
